#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nyu_var_list.h"

NyuVarList *nyu_var_list_prepend(NyuVarList *list,
                                 char *name,
                                 NyuVarData *data)
{
    NyuVarList *l;

    l = (NyuVarList *)malloc(sizeof(NyuVarList));
    if (!l) {
        perror("cannot alloc NyuVarList!");
        abort();
    }

    l->next = list;
    l->name = name;
    l->data = data;

    return l;
}

NyuVarList *nyu_var_list_find(NyuVarList *list, const char *name)
{
    NyuVarList *l;

    for (l = list; l; l = l->next) {
        if (strcmp(l->name, name) == 0)
            return l;
    }

    return NULL;
}

NyuVarData *nyu_var_data_new(NyuVarData *parent, long val, NyuScope *scope)
{
    NyuVarData *d;

    d = (NyuVarData *)malloc(sizeof(NyuVarData));
    if (!d) {
        perror("cannot alloc NyuVarData!");
        abort();
    }

    d->parent = parent;
    d->val = val;
    d->scope = scope;

    return d;
}
